﻿#pragma once

#ifndef QCEFCLIENT_H
#define QCEFCLIENT_H

#include <QtCore>

#include "QCefDebug.h"
#include "QCefHeader.h"
#include "QCefBrowserApp.h"
#include "QCefDelegate.h"


class QCefClient
    : public CefClient
    , public CefLifeSpanHandler
    , public CefLoadHandler
    , public CefDisplayHandler
    , public CefFocusHandler
    , public CefRenderHandler
    , public CefRequestHandler
    , public CefResourceRequestHandler
    , public CefDownloadHandler
{
public:
    typedef std::vector<CefRefPtr<CefBrowser>>  BrowserList;
    typedef QAtomicPointer<QCefDelegate>        DelegatePtr;

public:
    QCefClient(QCefApp * app, CefRefPtr<QCefDelegate> gate = nullptr);
    virtual ~QCefClient();

public:    
    BrowserList & getBrowserList() { return m_browserList; }

    CefRefPtr<QCefDelegate> getDelegate() { return m_delegate.loadAcquire(); }
    void setDelegate(CefRefPtr<QCefDelegate> val) { m_gate = val; m_delegate.storeRelease(m_gate.get()); }

    //CefRefPtr<CefBrowser> & getBrowser() { return m_browser; }

public:
    int finiClient();
    void closeAllBrowsers();

protected:
    QCefApp *               m_app;
    CefRefPtr<QCefDelegate> m_gate;
    DelegatePtr             m_delegate;
    BrowserList             m_browserList;
    //CefRefPtr<CefBrowser>   m_browser;

public:
    //////////////////////////////////////////////////////////////////////////
    //CefClient
    virtual CefRefPtr<CefAudioHandler> GetAudioHandler() override;

    //virtual CefRefPtr<CefCommandHandler> GetCommandHandler() override;

    virtual CefRefPtr<CefContextMenuHandler> GetContextMenuHandler() override;

    virtual CefRefPtr<CefDialogHandler> GetDialogHandler() override;

    virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() override;

    virtual CefRefPtr<CefDownloadHandler> GetDownloadHandler() override;

    virtual CefRefPtr<CefDragHandler> GetDragHandler() override;

    virtual CefRefPtr<CefFindHandler> GetFindHandler() override;

    virtual CefRefPtr<CefFocusHandler> GetFocusHandler() override;

    virtual CefRefPtr<CefFrameHandler> GetFrameHandler() override;

    virtual CefRefPtr<CefJSDialogHandler> GetJSDialogHandler() override;

    virtual CefRefPtr<CefKeyboardHandler> GetKeyboardHandler() override;

    virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() override;

    virtual CefRefPtr<CefLoadHandler> GetLoadHandler() override;

    virtual CefRefPtr<CefPrintHandler> GetPrintHandler() override;

    virtual CefRefPtr<CefRenderHandler> GetRenderHandler() override;

    virtual CefRefPtr<CefRequestHandler> GetRequestHandler() override;

    virtual bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
                                          CefRefPtr<CefFrame> frame,
                                          CefProcessId source_process,
                                          CefRefPtr<CefProcessMessage> message) override;


    //////////////////////////////////////////////////////////////////////////
    //CefLifeSpanHandler
    virtual bool OnBeforePopup(CefRefPtr<CefBrowser> browser,
                               CefRefPtr<CefFrame> frame,
                               const CefString& target_url,
                               const CefString& target_frame_name,
                               CefLifeSpanHandler::WindowOpenDisposition target_disposition,
                               bool user_gesture,
                               const CefPopupFeatures& popupFeatures,
                               CefWindowInfo& windowInfo,
                               CefRefPtr<CefClient>& client,
                               CefBrowserSettings& settings,
                               CefRefPtr<CefDictionaryValue>& extra_info,
                               bool* no_javascript_access) override;

    virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) override;


    virtual bool DoClose(CefRefPtr<CefBrowser> browser) override;

    virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) override;


    //////////////////////////////////////////////////////////////////////////
    //CefLoadHandler
    virtual void OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
                                      bool isLoading,
                                      bool canGoBack,
                                      bool canGoForward) override;

    virtual void OnLoadStart(CefRefPtr<CefBrowser> browser,
                             CefRefPtr<CefFrame> frame,
                             CefLoadHandler::TransitionType transition_type) override;

    virtual void OnLoadEnd(CefRefPtr<CefBrowser> browser,
                           CefRefPtr<CefFrame> frame,
                           int httpStatusCode) override;

    virtual void OnLoadError(CefRefPtr<CefBrowser> browser,
                             CefRefPtr<CefFrame> frame,
                             CefLoadHandler::ErrorCode errorCode,
                             const CefString& errorText,
                             const CefString& failedUrl) override;


    //////////////////////////////////////////////////////////////////////////
    //CefDisplayHandler
    virtual void OnAddressChange(CefRefPtr<CefBrowser> browser,
                                 CefRefPtr<CefFrame> frame,
                                 const CefString& url) override;

    virtual void OnTitleChange(CefRefPtr<CefBrowser> browser,
                               const CefString& title) override;

    virtual void OnFaviconURLChange(CefRefPtr<CefBrowser> browser,
                                    const std::vector<CefString>& icon_urls) override;

    virtual void OnFullscreenModeChange(CefRefPtr<CefBrowser> browser,
                                        bool fullscreen) override;

    virtual bool OnTooltip(CefRefPtr<CefBrowser> browser, CefString& text)  override;

    virtual void OnStatusMessage(CefRefPtr<CefBrowser> browser,
                                 const CefString& value) override;

    virtual bool OnConsoleMessage(CefRefPtr<CefBrowser> browser,
                                  cef_log_severity_t level,
                                  const CefString& message,
                                  const CefString& source,
                                  int line) override;

    virtual bool OnAutoResize(CefRefPtr<CefBrowser> browser,
                              const CefSize& new_size) override;

    virtual void OnLoadingProgressChange(CefRefPtr<CefBrowser> browser,
                                         double progress) override;

    virtual bool OnCursorChange(CefRefPtr<CefBrowser> browser,
                                CefCursorHandle cursor,
                                cef_cursor_type_t type,
                                const CefCursorInfo& custom_cursor_info) override;


    //////////////////////////////////////////////////////////////////////////
    //CefFocusHandler
    virtual void OnTakeFocus(CefRefPtr<CefBrowser> browser, bool next) override;

    virtual bool OnSetFocus(CefRefPtr<CefBrowser> browser, FocusSource source) override;

    virtual void OnGotFocus(CefRefPtr<CefBrowser> browser) override;


    //////////////////////////////////////////////////////////////////////////
    //CefRenderHandler
    virtual CefRefPtr<CefAccessibilityHandler> GetAccessibilityHandler() override;

    virtual bool GetRootScreenRect(CefRefPtr<CefBrowser> browser, CefRect& rect) override;

    virtual void GetViewRect(CefRefPtr<CefBrowser> browser, CefRect& rect) override;

    virtual bool GetScreenPoint(CefRefPtr<CefBrowser> browser,
                                int viewX,
                                int viewY,
                                int& screenX,
                                int& screenY) override;

    virtual bool GetScreenInfo(CefRefPtr<CefBrowser> browser,
                               CefScreenInfo& screen_info) override;

    virtual void OnPopupShow(CefRefPtr<CefBrowser> browser, bool show) override;

    virtual void OnPopupSize(CefRefPtr<CefBrowser> browser, const CefRect& rect) override;

    virtual void OnPaint(CefRefPtr<CefBrowser> browser,
                         PaintElementType type,
                         const RectList& dirtyRects,
                         const void* buffer,
                         int width,
                         int height) override;

    virtual void OnAcceleratedPaint(CefRefPtr<CefBrowser> browser,
                                    PaintElementType type,
                                    const RectList& dirtyRects,
                                    void* shared_handle) override;

    virtual bool StartDragging(CefRefPtr<CefBrowser> browser,
                               CefRefPtr<CefDragData> drag_data,
                               DragOperationsMask allowed_ops,
                               int x,
                               int y) override;

    virtual void UpdateDragCursor(CefRefPtr<CefBrowser> browser,
                                  DragOperation operation) override;

    virtual void OnScrollOffsetChanged(CefRefPtr<CefBrowser> browser,
                                       double x,
                                       double y) override;

    virtual void OnImeCompositionRangeChanged(CefRefPtr<CefBrowser> browser,
                                              const CefRange& selected_range,
                                              const RectList& character_bounds) override;

    virtual void OnTextSelectionChanged(CefRefPtr<CefBrowser> browser,
                                        const CefString& selected_text,
                                        const CefRange& selected_range) override;

    virtual void OnVirtualKeyboardRequested(CefRefPtr<CefBrowser> browser,
                                            TextInputMode input_mode) override;


   //////////////////////////////////////////////////////////////////////////
   //CefRequestHandler
    virtual bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser,
                                CefRefPtr<CefFrame> frame,
                                CefRefPtr<CefRequest> request,
                                bool user_gesture,
                                bool is_redirect) override;

    virtual bool OnOpenURLFromTab(CefRefPtr<CefBrowser> browser,
                                  CefRefPtr<CefFrame> frame,
                                  const CefString& target_url,
                                  CefRequestHandler::WindowOpenDisposition target_disposition,
                                  bool user_gesture) override;

    virtual CefRefPtr<CefResourceRequestHandler> GetResourceRequestHandler(CefRefPtr<CefBrowser> browser,
                                                                           CefRefPtr<CefFrame> frame,
                                                                           CefRefPtr<CefRequest> request,
                                                                           bool is_navigation,
                                                                           bool is_download,
                                                                           const CefString& request_initiator,
                                                                           bool& disable_default_handling) override;

    virtual bool GetAuthCredentials(CefRefPtr<CefBrowser> browser,
                                    const CefString& origin_url,
                                    bool isProxy,
                                    const CefString& host,
                                    int port,
                                    const CefString& realm,
                                    const CefString& scheme,
                                    CefRefPtr<CefAuthCallback> callback) override;

    virtual bool OnQuotaRequest(CefRefPtr<CefBrowser> browser,
                                const CefString& origin_url,
                                int64 new_size,
                                CefRefPtr<CefCallback> callback) override;

    virtual bool OnCertificateError(CefRefPtr<CefBrowser> browser,
                                    cef_errorcode_t cert_error,
                                    const CefString& request_url,
                                    CefRefPtr<CefSSLInfo> ssl_info,
                                    CefRefPtr<CefCallback> callback) override;

    virtual bool OnSelectClientCertificate(CefRefPtr<CefBrowser> browser,
                                           bool isProxy,
                                           const CefString& host,
                                           int port,
                                           const X509CertificateList& certificates,
                                           CefRefPtr<CefSelectClientCertificateCallback> callback) override;

    virtual void OnRenderViewReady(CefRefPtr<CefBrowser> browser) override;

    virtual void OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser,
                                           TerminationStatus status) override;

    virtual void OnDocumentAvailableInMainFrame(CefRefPtr<CefBrowser> browser) override;


   //////////////////////////////////////////////////////////////////////////
   //CefResourceRequestHandler
    virtual CefRefPtr<CefCookieAccessFilter> GetCookieAccessFilter(CefRefPtr<CefBrowser> browser,
                                                                   CefRefPtr<CefFrame> frame,
                                                                   CefRefPtr<CefRequest> request) override;

    virtual CefResourceRequestHandler::ReturnValue OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
                                                                        CefRefPtr<CefFrame> frame,
                                                                        CefRefPtr<CefRequest> request,
                                                                        CefRefPtr<CefCallback> callback) override;

    virtual CefRefPtr<CefResourceHandler> GetResourceHandler(CefRefPtr<CefBrowser> browser,
                                                             CefRefPtr<CefFrame> frame,
                                                             CefRefPtr<CefRequest> request) override;

    virtual void OnResourceRedirect(CefRefPtr<CefBrowser> browser,
                                    CefRefPtr<CefFrame> frame,
                                    CefRefPtr<CefRequest> request,
                                    CefRefPtr<CefResponse> response,
                                    CefString& new_url) override;

    virtual bool OnResourceResponse(CefRefPtr<CefBrowser> browser,
                                    CefRefPtr<CefFrame> frame,
                                    CefRefPtr<CefRequest> request,
                                    CefRefPtr<CefResponse> response) override;

    virtual CefRefPtr<CefResponseFilter> GetResourceResponseFilter(CefRefPtr<CefBrowser> browser,
                                                                   CefRefPtr<CefFrame> frame,
                                                                   CefRefPtr<CefRequest> request,
                                                                   CefRefPtr<CefResponse> response) override;

    virtual void OnResourceLoadComplete(CefRefPtr<CefBrowser> browser,
                                        CefRefPtr<CefFrame> frame,
                                        CefRefPtr<CefRequest> request,
                                        CefRefPtr<CefResponse> response,
                                        URLRequestStatus status,
                                        int64 received_content_length) override;

    virtual void OnProtocolExecution(CefRefPtr<CefBrowser> browser,
                                     CefRefPtr<CefFrame> frame,
                                     CefRefPtr<CefRequest> request,
                                     bool& allow_os_execution) override;


    //////////////////////////////////////////////////////////////////////////
    //CefDownloadHandler
    virtual void OnBeforeDownload(CefRefPtr<CefBrowser> browser,
                                  CefRefPtr<CefDownloadItem> download_item,
                                  const CefString& suggested_name,
                                  CefRefPtr<CefBeforeDownloadCallback> callback) override;

    virtual void OnDownloadUpdated(CefRefPtr<CefBrowser> browser,
                                   CefRefPtr<CefDownloadItem> download_item,
                                   CefRefPtr<CefDownloadItemCallback> callback) override;



private:
    IMPLEMENT_REFCOUNTING(QCefClient);
};

#endif // QCEFCLIENT_H
